package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.TsCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Override
	@TsCache(keyPrefix = "Album:cate")
	public List<JSONObject> getBaseCategroyList() {
		List<BaseCategoryView> baseCategoryview= baseCategoryViewMapper.selectList(null);
		Map<Long, List<BaseCategoryView>> categoryMap1 = baseCategoryview.stream().
				collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = categoryMap1.entrySet().iterator();
		List<JSONObject> list=new ArrayList<>();
		while (iterator.hasNext()) {
			JSONObject category1 = new JSONObject();
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			Long category1Id = entry.getKey();
			List<BaseCategoryView> categoryViewList = entry.getValue();
			category1.put("categoryId",category1Id);
			category1.put("categoryName",categoryViewList.get(0).getCategory1Name());
			//获取一级分类下的二级数据
			Map<Long, List<BaseCategoryView>> categoryMap2 = categoryViewList.stream().
					collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = categoryMap2.entrySet().iterator();
			List<JSONObject> categoryChild2=new ArrayList<>();
			while (iterator1.hasNext()){
				JSONObject category2 = new JSONObject();
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				Long category2Id = entry1.getKey();
				List<BaseCategoryView> categoryViewList2 = entry1.getValue();
				category2.put("categoryId",category2Id);
				category2.put("categoryName",categoryViewList2.get(0).getCategory2Name());
				//获取二级分类下三级数据(列表)
				List<JSONObject> categoryViewList3 = categoryViewList2.stream().map(baseCategoryView -> {
					JSONObject category3 = new JSONObject();
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());
				category2.put("categoryChild",categoryViewList3);
				categoryChild2.add(category2);
			}
			category1.put("categoryChild",categoryChild2);
			list.add(category1);
		}
		return list;
	}

	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		return baseAttributeMapper.selectAttribute(category1Id);
	}

	@Override
	public BaseCategoryView getCategoryView(Long category3Id) {
		BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(category3Id);
		return baseCategoryView;
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
		List<Long> category2IdList = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		List<BaseCategory3> baseCategory3List = baseCategory3Mapper.
				selectList(new LambdaQueryWrapper<BaseCategory3>()
						.in(BaseCategory3::getCategory2Id, category2IdList)
						.eq(BaseCategory3::getIsTop, 1).last(" limit 7"));
		return baseCategory3List;
	}

	@Override
	public List<BaseCategory1> findCategory1All() {
		return baseCategory1Mapper.selectList(null);
	}

	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		JSONObject category1 = new JSONObject();
		category1.put("categoryId",category1Id);
		List<BaseCategoryView> categoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
		category1.put("categoryName",categoryViewList.get(0).getCategory1Name());
		//根据一级分类Id获取二级分类
		List<JSONObject> categoryChild2List = new ArrayList<>();
		Map<Long, List<BaseCategoryView>> categroy2Map = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = categroy2Map.entrySet().iterator();
		while(iterator.hasNext()){
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			JSONObject category2 = new JSONObject();
			category2.put("categoryId", entry.getKey());
			category2.put("categoryName", entry.getValue().get(0).getCategory2Name());
			List<JSONObject> category3List = entry.getValue().stream().map(item -> {
				JSONObject category3 = new JSONObject();
				category3.put("categoryId", item.getCategory3Id());
				category3.put("categoryName", item.getCategory3Name());
				return category3;
			}).collect(Collectors.toList());
			category2.put("categoryChild", category3List);
			categoryChild2List.add(category2);
		}
		category1.put("categoryChild", categoryChild2List);
		return category1;
	}
}
